package com.mark.anto.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @Description: 系统属性条件判断的实现
 * @Author: Mark
 * @CreateDate: 2019/9/1 9:26
 * @Version: 3.00.001
 * @Copyright : 豆浆油条个人非正式工作室
 */
public class OnSystemPropertyCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        //以为键值方式获取到@SystemPropertyCondition中定义的属性,key为属性名，value为声明注解时注入的值
        //name = XXX
        //value =XXX
        Map<String, Object> attributes = metadata.getAnnotationAttributes(SystemPropertyCondition.class.getName());
        Assert.notNull(attributes,"获取注解属性为空！");

        String propertyName = String.valueOf(attributes.get("name"));
        String propertyValue = String.valueOf(attributes.get("value"));
        String javaPropertyValue = System.getProperty(propertyName);

        return propertyValue.equals(javaPropertyValue);

    }
}
